package o2o.web.actions.online;


import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ValidateUtil;
import com.mongodb.BasicDBObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class OnlineController {

    // 获取用户在线情况
    @RequestMapping(method = RequestMethod.POST, value = "/user/online/log/list")
    @ResponseBody
    public String user_online_info(Long shopId, String username, Long createTim, Integer size, Integer isNext) {
        // 获取用户在线日志
        Map<String, Object> query = new HashMap<>();

        if (ValidateUtil.isNotNull(username)) {
            query.put("username", username);
        }
        if (ValidateUtil.isID(shopId)) {
            query.put("shopId", shopId);
        }
        if (null == isNext) {
            isNext = 0;
        }
        if (null == size) {
            size = 20;
        }
        // 判断上一页或下一页
        BasicDBObject sort = null;
        if (createTim != null && createTim != 0) {
            if (1 == isNext) {
                query.put("createTim", new BasicDBObject("$lt", createTim)); // 下一页
                sort = new BasicDBObject("createTim", -1);
            } else {
                query.put("createTim", new BasicDBObject("$gt", createTim)); // 上一页
                sort = new BasicDBObject("createTim", 1);
            }
        }

        // 排序
        if (createTim != null && 0 == createTim) {
            sort = new BasicDBObject("createTim", -1);
        }

        List<HashMap<String, Object>> list = new ArrayList<>();
        // todo zhuwx
//        List<HashMap<String, Object>> list = MongoDaoImpl.getInstance().finds(ReleaseInformationMongo.onlineLogMongoDB, PojoTables.MG_ONLINE_LOG, query, size, sort);
//        if (ValidateUtil.isAbsList(list)) {
//            for (int i = 0; i < list.size(); i++) {
//                if (null == list.get(i)) {
//                    continue;
//                }
//                list.get(i).remove("_id");
//            }
//        }
        if (ValidateUtil.isAbsList(list) && !(1 == isNext)) {
            Collections.reverse(list);
        }
        return AjaxResult.successResult(list != null ? list.size() : 0, list);
    }
}
